package voxbox.wrapper;

import processing.core.PMatrix;
import processing.core.PVector;
import voxietest1.VoxiObject;

public class VoxiMesh extends VoxiObject {

	public String fileContent;
	public PVector pos;
	public PVector heading;
	
	public VoxiPoint3d right;
	public VoxiPoint3d down;
	public VoxiPoint3d forward;
	
	public byte[] bytes;
	
	public VoxiMesh(String fileName, PVector pos) {
		super();
		
		bytes = FileUtil.load(fileName);
		this.pos = new PVector(pos.x, pos.y, pos.z);
		this.heading = new PVector(0,0,1);
//		
//		String[] content = pApplet.loadStrings(fileName);
//		
//		fileContent = "";
//		
//		for(String str: content){
//			fileContent += str + "\n";
//		}
//		
//		this.pos = pos;

	}

	@Override
	public void mySetup() {
		// TODO Auto-generated method stub
	}

	@Override
	public void myUpdate() {
		// TODO Auto-generated method stub

	}

	@Override
	public void myDraw() {
//		voxi.voxi_drawspr(scene.frame, bytes, pos,
//				right, down, forward,
//				0x808080);
//		voxi.voxi_drawcube(scene.frame, pos,
//				right, down, forward, 1,
//				7);
		

	
		float f = pApplet.frameCount/10f;
		float scale = 0.10f;
//		
//		pApplet.resetMatrix();
//		pApplet.pushMatrix();
//		pApplet.scale(.2f);
//		pApplet.rotateZ(_angle);
//		pApplet.translate(-5,  -5, 0);
//		PMatrix m = pApplet.getMatrix();
//		pApplet.printMatrix();
//		pApplet.popMatrix();
//		
		
		PVector down = PVector.sub(new PVector(), heading);
		down.normalize();
			
		//PVector right = new PVector(1,0,0);
		//PVector forward = new PVector(0,1,0);
				
		PVector right = new PVector(1,0,0);
		PVector forward = down.cross(right);
		right = down.cross(forward);
		
		right.normalize();
		forward.normalize();
		
		right.mult(scale);
		down.mult(scale);
		forward.mult(scale);
		
		VoxiPoint3d pp = new VoxiPoint3d(pos);
		pp.x -= (right.x + down.x + forward.x)/2;
		pp.y -= (right.y + down.y + forward.y)/2;
		pp.z -= (right.z + down.z + forward.z)/2;
		
		VoxiPoint3d rr = new VoxiPoint3d(right);
		VoxiPoint3d dd = new VoxiPoint3d(down);
		VoxiPoint3d ff = new VoxiPoint3d(forward);

		
		/*
		rr.x = scale;
		dd.x = 0;
		ff.x = 0;
		
		rr.y = 0;
		dd.y = pApplet.cos(f) * scale;
		ff.y = -pApplet.sin(f) * scale;
		
		rr.z = 0;
		dd.z = pApplet.sin(f) * scale;
		ff.z = pApplet.cos(f) * scale;
		*/
		
		
		voxi.voxi_drawcube(scene.frame, pp, rr, dd, ff, 1, 7);
	}


	public void MoveTo(PVector position) {
		this.pos = position;
	}
	public void SetHeading(PVector heading) {
		this.heading = heading.get();
		this.heading.normalize();
	}
}
